import pygame
from qtawesome import font

from Actors.Lead_Actor import LeadActor
from Actors.Sprite import Sprite
from Data.Variable import GAME_TITLE, SCREEN_WIDTH, SCREEN_HEIGHT
from Dialog.Dialog_For_Systerm import TunnelTask1BeginDialog
from Scene.Base_Scene import ScenePassStatus
from Scene.Fail_Scene import FailScene
from Scene.Main_Scene import MainScene
from Scene.Task_Scene.Task1 import Task1Scene
from Scene.Task_Scene.Task2 import Task2Scene
from Scene.Task_Scene.Task3 import Task3Scene
from Scene.Welcome_Scene import WelcomeScene
from Scene.Win_Scene import WinScene


class Game:
    def __init__(self):
        # 初始化pygame
        pygame.init()
        # 设置显示区域
        pygame.display.set_caption(GAME_TITLE)
        # 创建游戏的窗口 1280*720 根据要显示图片的大小设置
        self.surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        # 加载当前场景
        self.current_scene = Task1Scene()
        # 获取游戏时钟
        self.clock = pygame.time.Clock()

    def run(self):
        # 游戏循环
        while True:
            down_flag = False
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    exit()
                if event.type == pygame.KEYDOWN:
                    down_flag = True
            # 获得键盘事件
            key_list = pygame.key.get_pressed()
            # 执行各种行为
            screen = self.current_scene.make_surface()
            self.surface.blit(screen, (0, 0))
            # 通过时钟对象指定循环频率
            self.clock.tick(25)
            # 3. 场景中人物的运动
            self.current_scene.run(down_flag, key_list)
            # 4. 切换场景
            next_status, next_scene_name = self.current_scene.get_next_status()
            if next_status != ScenePassStatus.normal:
                if next_scene_name:
                    next_scene = globals()[next_scene_name]()
                    self.current_scene = next_scene
                else:
                    break
            # 调用flip方法更新显示,也可以使用update方法
            pygame.display.flip()
